// 一个模块就是一个对象(小store)

import { reqGoodsDetail } from "@/api";

export default {
  state: {
    goodsDetail: {},
  },
  mutations: {
    UPDATE_GOODSDETAIL(state, goodsDetail) {
      state.goodsDetail = goodsDetail;
    },
  },
  actions: {
    async getGoodsDetail({ commit }, skuId) {
      const res = await reqGoodsDetail(skuId);
      if (res.code === 200) {
        commit("UPDATE_GOODSDETAIL", res.data);
      }
    },
  },
  getters: {
    // 咱们或空数组或者或空对象 目的是为了防止组件中使用未请求回来的数据是undefined的这种可能
    categoryView(state) {
      return state.goodsDetail.categoryView || {};
    },
    skuInfo(state) {
      return state.goodsDetail.skuInfo || {};
    },
    spuSaleAttrList(state) {
      return state.goodsDetail.spuSaleAttrList || [];
    },
  },
};
